package cn.k.controller;

import cn.k.domain.Appointment;
import cn.k.domain.Business;
import cn.k.domain.SystemDictionary;
import cn.k.qo.AppointmentQueryObject;
import cn.k.qo.JSONResult;
import cn.k.qo.QueryObject;
import cn.k.service.IAppointmentService;
import cn.k.service.IBusinessService;
import cn.k.service.ISystemDictionaryService;
import cn.k.util.RequirePermission;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/appointment")
public class AppointmentController {

    @Autowired
    private IAppointmentService appointmentService;

    @Autowired
    private IBusinessService businessService;

    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    //处理分页查询页面请求
    @RequestMapping("/list")
    //@RequiresPermissions(value={"appointment:list","预约查询"},logical = Logical.OR)
    @RequirePermission(name = "预约查询",expression = "appointment:list")
    public String list(@ModelAttribute("qo") AppointmentQueryObject qo, Model model){
        PageInfo<Appointment> pageInfo = appointmentService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        //查询预约门店
        List<Business> businesses = businessService.selectAll();
        model.addAttribute("businesses",businesses);


        //业务分类
        List<SystemDictionary> category = systemDictionaryService.queryByService("business");
        model.addAttribute("category",category);


        return "appointment/list";


    }


    //处理删除请求
    //@RequirePermission(name = "预约删除",expression = "appointment:delete")
    @RequiresPermissions(value={"appointment:delete","预约删除"},logical = Logical.OR)
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id!=null){
            appointmentService.delete(id);
        }
        return "redirect:/appointment/list";
    }


    //处理添加或者更新的请求
    @RequirePermission(name ="预约编辑",expression = "appointment:saveOrUpdate")
    //@RequiresPermissions(value={"appointment:saveOrUpdate","预约编辑"},logical = Logical.OR)
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Appointment appointment){

        //判断有没有id
        if (appointment.getId()==null){
            appointmentService.insert(appointment);
        }else {
            appointmentService.update(appointment);
        }
        return "redirect:/appointment/list";
    }


    @RequestMapping("/updateStatus")
    @ResponseBody
    public JSONResult updateStatus(Long id, int status){
        Appointment appointment = appointmentService.get(id);
        if (appointment.getStatus()==0){
            appointmentService.updateStatus(id,status);
            return new JSONResult(true,"预约/取消成功");
        }else if (status==3){
            appointmentService.updateStatus(id,status);
            return  new JSONResult(true,"更改归档状态成功");
        }else {
            return new JSONResult(false,"当前状态不是待确认");
        }

    }

    //处理添加或者更新的请求
    @RequirePermission(name ="预约编辑",expression = "appointment:saveOrUpdate")
    //@RequiresPermissions(value={"appointment:saveOrUpdate","预约编辑"},logical = Logical.OR)
    @RequestMapping("/save")
    public String save(Appointment appointment){

        //判断有没有id
        if (appointment.getId()==null){
            appointmentService.insert(appointment);
        }
        return "redirect:/index";
    }


}
